package org.solar.web;

import javax.servlet.http.HttpServletRequest;

public class WebUtil {

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-real-ip");//先从nginx自定义配置获取
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static UserAgent getUserAgent(HttpServletRequest request) {
        return UserAgentUtil.getUserAgent(request.getHeader("user-agent"));
    }
    public static String getBrowserType(HttpServletRequest request) {
        UserAgent userAgent=getUserAgent(request);
        if (userAgent==null){
            return null;
        }
        return  userAgent.getBrowserType();
    }
    public static String getBrowserInfo(HttpServletRequest request) {
        UserAgent userAgent=getUserAgent(request);
        if (userAgent==null){
            return null;
        }
        return  userAgent.getBrowserType()+"-"+userAgent.getBrowserVersion();
    }
    public static String getOSType(HttpServletRequest request) {
        UserAgent userAgent=getUserAgent(request);
        if (userAgent==null){
            return null;
        }
        return  userAgent.getPlatformType();
    }
    public static String getOSInfo(HttpServletRequest request) {
        UserAgent userAgent=getUserAgent(request);
        if (userAgent==null){
            return null;
        }
        if (userAgent.getPlatformVersion()!=null&&userAgent.getPlatformSeries()!=null){
            return  userAgent.getPlatformType()+userAgent.getPlatformSeries()+userAgent.getPlatformVersion();
        }
        if (userAgent.getPlatformSeries()!=null){
            return  userAgent.getPlatformType()+userAgent.getPlatformSeries();
        }
        if (userAgent.getPlatformVersion()!=null){
            return  userAgent.getPlatformType()+userAgent.getPlatformVersion();
        }
        return  userAgent.getPlatformType();
    }

}
